angular.module('goods-detail', ['share-model'])

.controller 'GoodsDetailCtrl', ($scope, $state, $http,showToast, $ionicHistory, $stateParams,$localStorage, wfCache,$ionicViewSwitcher,$ionicPopup,goodsInfo,userData,paginationFactory,shareModel) ->
    $scope.banners = []
    $scope.goodId = $stateParams.goodId
    $scope.isOnLine = goodsInfo.isOnLine
    console.log $scope.isOnLine
    shareModel.set [
        {img:'img/timeline.png',text:'分享到朋友圈',func: -> $scope.goShare0()}
        {img:'img/wechat.png',text:'分享给朋友',func: -> $scope.goShare1()}
    ]

    $scope.goShare0 = ->
        console.log 56789
        $http.get('/seller/',params:{action:'shareproduct',shopid:userData.getToken(),proid:$stateParams.goodId})
        .success (resp)->
            if resp?.status == '0'
                $scope.shareData = resp
                Wechat.share({
                            message: {
                                title: $scope.shareData.title
                                description: $scope.shareData.description
                                thumb: $scope.shareData.shoplogo
                                media: {
                                    type: Wechat.Type.LINK,
                                    webpageUrl: $scope.shareData.url
                                }
                            },
                            scene: Wechat.Scene.TIMELINE
                        },
                    ()->console.log('分享成功~'),
                    (reason) ->console.log(reason)
                )
            else
                $scope.errorMsg = resp.msg
        .error ->
            showToast '网络异常,请检查网络是否连接'

    $scope.goShare1 = ->
        $http.get('/seller/',params:{action:'shareproduct',shopid:userData.getToken(),proid:$stateParams.goodId})
        .success (resp)->
            if resp?.status == '0'
                $scope.shareData = resp
                Wechat.share({
                            message: {
                                title: $scope.shareData.title
                                description: $scope.shareData.description
                                thumb: $scope.shareData.shoplogo
                                media: {
                                    type: Wechat.Type.LINK,
                                    webpageUrl: $scope.shareData.url
                                }
                            },
                            scene: Wechat.Scene.SESSION
                        },
                    ()->console.log('分享成功~'),
                    (reason) ->console.log(reason)
                )
            else
                $scope.errorMsg = resp.msg
        .error ->
            showToast '网络异常,请检查网络是否连接'

    _.extend $scope,
        $http.get('/updshop/',params:{action:'productinfo',id:$scope.goodId,isupk:'0'})
        .success (resp) ->
            if resp?.status == '0'
                $scope.goods = resp
                console.log $scope.goods
                goodsInfo.goods = $scope.goods
                $scope.banners = resp.imgs
                $scope.images = resp.images
                console.log $scope.banners
            else
                showToast '没有该商品'
        .error ->
            showToast '网络异常,请检查网络是否连接'
        goBack: ->
            $ionicViewSwitcher.nextDirection('back')
            $ionicHistory.goBack()

        getShareInfo: (e) ->
            shareModel.show e

        goGoodsEdit: ->
            $state.go 'tab.goods-edit'
        takeOff: ->
            $http.post('/updshop/',{action:'proshelves',userid:userData.getToken(),id:$scope.goodId,issale:'0'})
            .success (resp) ->
                if resp?.status == '0'
                    showToast '下架成功'
                    $ionicHistory.goBack()
                    $scope.isOnLine =! $scope.isOnLine
                else
                    showToast '没有该商品'
            .error ->
                showToast '网络异常,请检查网络是否连接'
        takeOn: ->
            $http.post('/updshop/',{action:'proshelves',userid:userData.getToken(),id:$scope.goodId,issale:'1'})
            .success (resp) ->
                if resp?.status == '0'
                    showToast '上架成功'
                    $ionicHistory.goBack()
                    $scope.isOnLine =! $scope.isOnLine
                else
                    showToast '没有该商品'
            .error ->
                showToast '网络异常,请检查网络是否连接'

        remove: ->
            $http.post('/updshop/',{action:'delproduct',userid:userData.getToken(),id:$scope.goodId})
            .success (resp) ->
                if resp?.status == '0'
                    showToast '删除成功'
                    _.each $localStorage.tabMarketGoodList,(item)->
                        if ''+item.id == ''+$scope.goods.upkproid
                            item.isadd = 0
                    $ionicHistory.goBack()
                else
                    showToast '没有该商品'
            .error ->
                showToast '网络异常,请检查网络是否连接'
    $scope.$on '$stateChangeSuccess', ->
        if $state.is 'tab.goods-detail'
            $http.get('/updshop/',params:{action:'productinfo',id:$scope.goodId,isupk:'0'})
            .success (resp) ->
                if resp?.status == '0'
                    $scope.goods = resp
                    console.log $scope.goods
                    goodsInfo.goods = $scope.goods
                    $scope.banners = resp.imgs
                    $scope.images = resp.images
                    console.log $scope.banners
                else
                    showToast '没有该商品'
            .error ->
                showToast '网络异常,请检查网络是否连接'
    $scope.$on '$ionicView.enter', ->
        $scope.isOnLine = goodsInfo.isOnLine
        console.log $scope.isOnLine


